package android.demo.activity.dialog;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.demo.BaseActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class DialogActivity extends BaseActivity {

	private final int CUSTOM_DIALOG = 0;
	private final int ALERT_DIALOG = 1;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle("对话框");
		ListView mListView = new ListView(this);  
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_checked,  
				new String[] {"借助Dialog类，或它的子类（如AlertDialog）", "使用Activity的对话框主题"}
		);  
		mListView.setAdapter(adapter);  
		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				switch (position) {
					case CUSTOM_DIALOG :
						MyDialog myDialog = new MyDialog(DialogActivity.this, new MyDialogListener() {
							
							@Override
							public void onOkClick(String name) {
								Toast.makeText(DialogActivity.this, name, 3000).show();
							}
							
							@Override
							public void onCancelClick() {
								
							}
						});
						myDialog.show();
						break;
					case ALERT_DIALOG :
						AlertDialog dialog = new AlertDialog.Builder(DialogActivity.this).create();
						dialog.setMessage("Do you play cricket?");
						dialog.setButton("Yes", new OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								Toast.makeText(DialogActivity.this, "It's ok", 3000).show();
							}
						});
						dialog.setButton2("No", new OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								Toast.makeText(DialogActivity.this, "It's cancel", 3000).show();
							}
						});
						dialog.show();

						break;
				}

			}
		});
		setContentView(mListView); 
	}
}

